<?php
class dbbackMod extends commonMod{

	public function __construct()
	{
		parent::__construct();

	}

	public function index(){
		$db=new Dbbak($this->config['DB_HOST'],$this->config['DB_USER'],$this->config['DB_PWD'],$this->config['DB_NAME'],'utf8','../data/dbback/');
		$table	= $db->getTables($this->config['DB_NAME']);
		$this->assign('list',$this->getFileName());
		$this->assign('dbname',time());
		$this->assign('table' ,$table);
		$this->display();
	}
	//开始备份数据
	public function backup(){

		@set_time_limit(0);
		$db= new Dbbak($this->config['DB_HOST'],$this->config['DB_USER'],$this->config['DB_PWD'],$this->config['DB_NAME'],'utf8','../data/dbback/');

		$list=$db->getTables($this->config['DB_NAME']);//获取数据库表名
		$back=intval($_POST['back']);
		$table=$_POST['table'];
		$dbsize=$_POST['size'];
		if($back){
			
			$table=$list;
		}
		if(empty($table)) 
			$this->error('请选择需要备份的表!');

		//备份并生成sql文件
	    if(!$db->exportSql($table,$dbsize))
	    {
	        $this->error('备份失败');
	    }
	    else
	    {
	        $this->success('备份成功');
	    }
	}

	//获取备份文件列表
	public function getFileName()
	{
		$f_open = "../data/dbback/";
		if(is_dir($f_open))
		{
			$dir = opendir($f_open);
			while(false!=$file=readdir($dir))
			{    
				if($file!='.' && $file!='..' && !is_dir($f_open."\\".$file))
				{
					$file		= $file;
					$arr_file1[]= $file;
				}
			}
		}
		closedir($dir);
		if($arr_file1)
		{
			$i = 0;
			foreach($arr_file1 as &$val)
			{
				$path = '../data/dbback/'.$val;
				$arr_file2[$i]['name']= $val;
				$arr_file2[$i]['size']= ceil(filesize($path)/1024);
				$arr_file2[$i]['time']= fileatime($path);
				$i++;
			}
		}
		return $arr_file2;	
	}

	//恢复已存在备份
	public function recover()
	{
		@set_time_limit(0);
		$db		= new Dbbak($this->config['DB_HOST'],$this->config['DB_USER'],$this->config['DB_PWD'],$this->config['DB_NAME'],'utf8','../data/dbback/');
		$path= $_GET['path'];
		if(empty($path)) $this->error('参数错误');
		if($db->importSql($path))
		{
			$this->success('数据导入成功！');
		}
		else{
			$this->error('数据导入失败！');
		}
	}
	public function del(){

		$file=$_GET['file'];
		$files = '../data/dbback/'.$file;
		// dump($files);
		if(empty($file)) $this->error('参数错误');
		if(unlink($files))
		$this->success('删除成功');
		else $this->error('删除失败');
	}
	
}
?>